//
//  HZJNavigationBar.swift
//  HZJNavigationView
//
//  Created by HZJ on 2017/2/20.
//  Copyright © 2017年 HZJ. All rights reserved.
//

import UIKit
var SCREENW :CGFloat{
    return UIScreen.main.bounds.width
}
var SCREENH :CGFloat{
    return UIScreen.main.bounds.height
}

fileprivate var NAVBARW:CGFloat{
   return SCREENW
}
fileprivate var NAVBARH:CGFloat{
    return 44
}
class HZJNavigationBar: UIView {
    
    /// 背景色
    override var backgroundColor: UIColor?{
        set{
            myBackgroundImg.backgroundColor = newValue
        }
        get{
            return myBackgroundImg.backgroundColor
        }
    }
    
    /// 背景透明度
    var backgroundAlpha:CGFloat?{
        set{
            guard newValue != nil else {
                return
            }
            myBackgroundImg.alpha = newValue!
        }
        get{
            return myBackgroundImg.alpha
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUIAndBaseInfo()
    }
    /// 添加控件和基础数据
    fileprivate func setUpUIAndBaseInfo(){
        backgroundColor = .white
        
        addSubview(myBackgroundImg)
        addSubview(myTitleLable)
        addSubview(myLeftBtn)
        addSubview(myRightBtn)
        
        myLeftBtn.frame = CGRect(x: 15, y: (NAVBARH - 40) * 0.5, width: 40, height: 40)
        myRightBtn.frame = CGRect(x: NAVBARW - 40 - 15, y:(NAVBARH - 40) * 0.5, width: 40, height: 40)
        myTitleLable.frame = CGRect(x: 15 + 40 + 5, y: 0, width: NAVBARW - (15 + 40 + 5) * 2, height: NAVBARH)
        
        myLeftBtn.setTitle("left", for: UIControlState.normal)
        myRightBtn.setTitle("right", for: UIControlState.normal)
    }
    
    
    /// 设置背景图
    ///
    /// - Parameters:
    ///   - backgroundImage: 背景图
    ///   - contentMode: 图片的剪切模式
     func setBackgroundImage(_ backgroundImage: UIImage?, contentMode:UIViewContentMode?){
        myBackgroundImg.image = backgroundImage
        if contentMode != nil {
            myBackgroundImg.contentMode = contentMode!
        }
    }
//    open func setBackgroundImage(_ backgroundImage: UIImage?, for barMetrics: UIBarMetrics)

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        myBackgroundImg.frame = CGRect(x: 0, y: -(frame.maxY - frame.height), width: frame.width, height: frame.maxY)
    }
    
    /// 背景视图
    fileprivate lazy var myBackgroundImg:UIImageView = {
       let imgV = UIImageView()
        return imgV
    }()
     ///导航栏左侧按钮
     lazy var myLeftBtn:UIButton = {
        let btn = UIButton()
        btn.setTitleColor(UIColor.black, for: UIControlState.normal)
        btn.titleLabel?.textAlignment = .center
        return btn
    }()
    
     ///导航栏右侧按钮
    lazy var myRightBtn:UIButton = {
        let btn = UIButton()
        btn.setTitleColor(UIColor.black, for: UIControlState.normal)
        btn.titleLabel?.textAlignment = .center
        return btn
    }()
     ///导航栏标题
    lazy var myTitleLable:UILabel = {
        let tv = UILabel()
        tv.textAlignment = .center
        tv.font = UIFont.systemFont(ofSize: 17, weight: UIFont.Weight(rawValue: 0.2))
        return tv
    }()
}
